using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace qPrintComponent
{
    public sealed class PrinterInfo2 : qSafePrinterInfo
    {
        private DevMode _DevMode;
        
        internal PrinterInfo2(SafeHandle PrinterHandle)
            : base(PrinterHandle,2)
        {
            if (!IsInvalid)
            {
                _DevMode = new DevMode(GetIntPtrField(7));
            }
        }

        
        public override string ToString()
        {
            return PrintProcessor;
        }
        //0
        [Description(@"The server that controls the printer. If this string is NULL, the printer is controlled locally.")]
        public string ServerName
        {
            get { return GetStringField(0); }
        }
        //1
        [Description(@"The name of the printer.")]
        [DisplayName("Printer Name")]
        public string PrinterName2
        {
            get { return GetStringField(1); }            
        }
        //2
        [Description(@"The sharepoint for the printer.")]
        public string ShareName
        {
            get { return GetStringField(2); }
        }
        //3
        [Description(@"The port(s) used to transmit data to the printer. If a printer is connected to more than one port, the names of each port must be separated by commas (for example, 'LPT1:,LPT2:,LPT3:').")]
        public string PortName
        {
            get { return GetStringField(3); }
        }
        //4
        [Description(@"The name of the printer driver.")]
        public string DriverName
        {
            get { return GetStringField(4); }
        }
        //5
        [Description(@"A brief description of the printer.")]
        public string Comment
        {
            get { return GetStringField(5); }
            set { if (value != Comment) SetStringField(5, value); }
        }
        //6
        [Description(@"The physical location of the printer (for example, 'Bldg. 38, Room 1164'). ")]
        public string Location
        {
            get { return GetStringField(6); }
            set { if (value != Location) SetStringField(6, value); }
        }
        //7
        [TypeConverter(typeof(ExpandableObjectConverter))]
        [Description(@"The Device Mode of the printer.")]
        public DevMode DevMode
        {
            get { return _DevMode; }
        }
        //8
        [Description(@"The name of the file used to create the separator page. This page is used to separate print jobs sent to the printer.")]
        public string SeperatorFile
        {
            get { return GetStringField(8); }
        }
        //9
        [Description(@"The name of the print processor used by the printer.")]
        public string PrintProcessor
        {
            get { return GetStringField(9); }
        }
        //10
        [Description(@"The data type used to record the print job.")]
        public string DataType
        {
            get { return GetStringField(10); }
        }
        //11
        [Description(@"The default print-processor parameters.")]
        public string Parameters
        {
            get { return GetStringField(11); }
        }
        //12
        [Description(@"A SECURITY_DESCRIPTOR structure for the printer. This member may be NULL.")]
        [Browsable(false)]
        public int SecurityDescriptor
        {
            get { return GetIntField(12); }
        }
        //13
        [Description("Printer Attributes.")]
        [DisplayName("Printer Attributes")]
        public qAttributes Attributes
        {
            get { return new qAttributes(this, GetIntField(13)); }
        }
        //14
        [Description(@"Specifies a priority value that the spooler uses to route print jobs.")]
        public int Priority
        {
            get { return GetIntField(14); }
            set { if (value != Priority) SetIntField(14, value); }
        }
        //15
        [Description(@"Specifies the default priority value assigned to each print job.")]
        public int DefaultPriority
        {
            get { return GetIntField(15); }
            set { if (value != DefaultPriority) SetIntField(15, value); }
        }
        //16
        [Description(@"Specifies the earliest time at which the printer will print a job.")]
        public TimeSpan StartTime
        {
            get { return GetTime(GetIntField(16));}
            set { if (value != StartTime) SetTime(value, 16); }
        }
        //17
        [Description(@"Specifies the latest time at which the printer will print a job.")]
        public TimeSpan UntilTime
        {
            get { return GetTime(GetIntField(17)); }
            set { if (value != UntilTime) SetTime(value, 17); }
        }
        //18
        [Description(@"The Status of the Printer.")]
        public qPrinterStatus Status
        {
            get { return new qPrinterStatus(GetIntField(18)); }
        }
        //19
        [Description(@"The number of print jobs that have been queued for the printer.")]
        public int Jobs
        {
            get { return GetIntField(19); }
        }
        //20
        [Description(@"The average number of pages per minute that have been printed on the printer.")]
        public int AveragePPM
        {
            get { return GetIntField(20); }
        }
        
        
        private TimeSpan GetTime(int intvalue)
        {
            int num1 = qStatic.hourdiv + intvalue;
            int num2 = num1 / 60;
            int num3 = num1 - (num2 * 60);
            if (num2 > 24) num2 -= 24;
            return new TimeSpan(num2, num3, 0);
        }
        private void SetTime(TimeSpan ts, int offset)
        {
            int num1 = (ts.Hours * 60) + ts.Minutes;
            num1 -= qStatic.hourdiv;
            if (num1 < 0) num1 += (24 * 60);
            SetIntField(offset, num1);               
        }
    }
}
